package com.syni.mdd.yhd.common.all.controller.protect;

import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckParams;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.entity.bo.user.CheckUserBo;
import com.syni.mdd.yhd.common.all.service.BmsSearchLogService;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.yhd.common.all.utils.RequestParamUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @className BmsSearchLogController
 * @description 搜索结果controller
 * @Author cfx
 * @DATE 2019/4/16 10:10
 * @VERSION 1.0
 **/
@RestController
@RequestMapping("/searchLog")
public class BmsSearchLogController {

    @Autowired
    BmsSearchLogService bmsSearchLogService;

    @CheckParams(checkAuth = true)
    @PostMapping("/getUserSearchHistory")
    public Map<String,Object> getUserSearchHistory(@RequestBody Map<String,Object> paramsMap){
        String userIdentify = paramsMap.get("userIdentify").toString();
        Integer userId = Integer.parseInt(userIdentify);
        Integer searchNum = null;
        if(paramsMap.containsKey("search_num") && !"".equals(paramsMap.get("search_num").toString())){
            searchNum = Integer.parseInt(paramsMap.get("search_num").toString());
        }
        List<String> bmsSearchLogList = bmsSearchLogService.bmsSearchLogList(userId,searchNum);
        if(bmsSearchLogList == null){
            return ResultMapHelper.failure("查询出错！");
        }
        return ResultMapHelper.success(bmsSearchLogList,"查询成功！");
    }



    @PostMapping("/getUserSearchHot")
    public Map<String,Object> getUserSearchHot(@RequestBody ParamsMap paramsMap){
        Integer searchNum = paramsMap.getInteger("search_num");
        List<String> bmsSearchLogList = bmsSearchLogService.getUserSearchHot(searchNum);
        return ResultMapHelper.success(bmsSearchLogList,"查询成功");
    }

    @CheckParams(checkAuth = true)
    @PostMapping("/delUserSearchHistory")
    public Map<String,Object> delUserSearchHistory(@RequestBody Map<String,Object> paramsMap){
        String userIdentify = paramsMap.get("userIdentify").toString();
        Integer userId = Integer.parseInt(userIdentify);
        bmsSearchLogService.delUserSearchHistroy(userId);
        return ResultMapHelper.success(null,"操作成功");
    }

    @CheckParams
    @PostMapping("/recordSearchClient")
    public Map<String,Object> recordSearchClient(@RequestBody ParamsMap paramsMap){
        Integer userId = null;
        CheckUserBo checkUserBo = RequestParamUtils.checkUserBo();
        //默认使用设备标识
        if(checkUserBo.getCheckStatus().equals(ResultCode.CODE_NORMAL)){
            userId = checkUserBo.getIntegerByUserId();
        }
        if(userId == null){
            return ResultMapHelper.success(null, ResultMsg.HAND_SUCCESS);
        }
        Integer businessId = paramsMap.getInteger("businessId");
        Integer videoId = paramsMap.getInteger("videoId");
        bmsSearchLogService.recordSearchClient(userId,businessId,videoId);
        return ResultMapHelper.success(null, ResultMsg.HAND_SUCCESS);
    }
}
